package com.escool.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.escool.dto.EstudianteDetalleDTO;
import com.escool.model.TbExpediente;
import com.escool.model.TbExpedienteMateriaHasRotacionesActividad;
import com.escool.model.TbMensaje;
import com.escool.model.TbRecord;
import com.escool.model.VwUsuarios;
import com.escool.service.ExpedienteService;
import com.escool.service.FindService;
import com.escool.service.ProfAlumnoService;
import com.escool.service.RotacionActividadService;
import com.escool.service.TipoRecordService;

@Controller
public class ProfAlumnoController {
	
	@Autowired
	FindService findService;
	
	@Autowired 
	ExpedienteService expedienteService;
	
	@Autowired
	ProfAlumnoService profAlumnoService;

	@Autowired
	RotacionActividadService rotacionActividadService;
	
	@Autowired
	TipoRecordService tipoRecordService;
	
	
	@RequestMapping("showProfesorAlumno")
	public String showCatActividad(Map<String, Object> map){
		try
		{
			User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			 map.put("estudianteDetalleDTO", new EstudianteDetalleDTO());
			map.put("vwAlumnoNotasList", findService.listVwAlumnoNotas( user.getUsername()));
			
			//System.out.println("nombre logado" + user.getUsername());
			return "/profesorAlumno";
		}catch(Exception ex){
			ex.printStackTrace();
			return "/login";	
			}
		
	}
	
	
	
	@RequestMapping("showRecord")
	public String showRecord(Map<String, Object> map,HttpServletRequest request,@ModelAttribute("rec") @Valid TbRecord tbRecord,BindingResult result){
		//String id = request.getParameter("id");
		//System.out.println(id);
		//TbExpediente tbExpediente = expedienteService.findById(Integer.valueOf(id));
		map.put("rec", new TbRecord());
		map.put("recList", profAlumnoService.listAllRecord());
		map.put("tiporecList", tipoRecordService.listAll());
		return "record";
	}
	
	@RequestMapping("showRecord_2")
	public String showRecord2(Map<String, Object> map,HttpServletRequest request,@ModelAttribute("estudianteDetalleDTO") @Valid EstudianteDetalleDTO estudianteDetalleDTO, BindingResult result){
		//User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		TbRecord tbRecord = new TbRecord();
		
		
		map.put("rec", tbRecord);
		map.put("recList", profAlumnoService.listAllRecord());
		map.put("tiporecList", tipoRecordService.listAll());
		return "/record";
	}
	
	@RequestMapping("addRec")
	public String addRec(Map<String, Object> map,HttpServletRequest request,@ModelAttribute("msg") @Valid TbRecord tbRecord,BindingResult result){
		if(!result.hasErrors()){
			
			profAlumnoService.addRecord(tbRecord);
			return "redirect:/showRecord";
		}
		return "/showRecord";
	}
	
	@RequestMapping("showMsgInbox")
	public String showMsgIn(Map<String, Object> map,HttpServletRequest request,@ModelAttribute("msg") @Valid TbMensaje tbMensaje, BindingResult result){
		String id = request.getParameter("id");
		System.out.println(id);
		TbExpediente tbExpediente = expedienteService.findById(Integer.valueOf(id));
		tbMensaje.setTbExpediente(tbExpediente);
		map.put("msg", tbMensaje);
		map.put("msgList", profAlumnoService.listMsg());
		return "/inbox";
	}
	
	
	@RequestMapping("showMsgInbox_2")
	public String showRotacionesService(Map<String, Object> map,HttpServletRequest request,@ModelAttribute("estudianteDetalleDTO") @Valid EstudianteDetalleDTO estudianteDetalleDTO, BindingResult result){
		User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		TbMensaje tbMensaje = new TbMensaje();
		TbExpediente tbExpediente = expedienteService.findById(Integer.valueOf(estudianteDetalleDTO.getIdExpediente()));
		tbMensaje.setTbExpediente(tbExpediente);
		tbMensaje.setCodEmisario(user.getUsername());
		map.put("msg", tbMensaje);
		map.put("msgList", profAlumnoService.listMsg());
		return "/inbox";
	}
	
	@RequestMapping("addMsg")
	public String addMsg(Map<String, Object> map,HttpServletRequest request,@ModelAttribute("msg") @Valid TbMensaje tbMensaje, BindingResult result){
		if(!result.hasErrors()){
			profAlumnoService.addMsg(tbMensaje);
			return "redirect:/showMsgInbox?id="+tbMensaje.getTbExpediente().getIdExpediente();
		}
		return "";
	}
	
	@RequestMapping("showNotas")
	public String showNotas(Map<String, Object> map,HttpServletRequest request,@ModelAttribute("nota") @Valid TbExpedienteMateriaHasRotacionesActividad nota, BindingResult result){		
		String id = request.getParameter("id");
		map.put("nota", nota);
		map.put("notasList", expedienteService.listActividadByExpedienteId(Integer.valueOf(id)));
		return "notas";
	}	
	
	@RequestMapping("showNotas_2")
	public String showNotas2(@ModelAttribute("estudianteDetalleDTO") @Valid EstudianteDetalleDTO estudianteDetalleDTO, BindingResult result,Map<String, Object> map,HttpServletRequest request){
		map.put("nota", new TbExpedienteMateriaHasRotacionesActividad());
		map.put("notasList", expedienteService.listActividadByExpedienteId(Integer.valueOf(estudianteDetalleDTO.getIdExpediente())));
		
		//TbExpediente tbExpediente = expedienteService.findById(Integer.valueOf(estudianteDetalleDTO.getIdExpediente()));
		return "notas";
	}
	
	@RequestMapping("updateNota")
	public String updateNota(Map<String, Object> map,HttpServletRequest request,@ModelAttribute("nota") @Valid TbExpedienteMateriaHasRotacionesActividad nota,BindingResult result){
		if(result.hasErrors()){
			map.put("notasList", expedienteService.listActividadByExpedienteId(nota.getTbExpedienteHasMateria().getTbExpediente().getIdExpediente()));
			return "notas";
			
		}
		expedienteService.updateTbExpedienteMateriaHasRotacionesActividad(nota);
		return "redirect:/index";
	}
	

}
